/********************
* Procedure codes
*
* Input: spreadsheet tables with ICD codes listed and label
* Useful if these need to be checked/updated later
* Also include 'version' column incase we need to check earlier ICD editions
* 
* Output: variable list for procedure codes 
*
*******************/

libname codes 'C:\Users\kcha0642\Documents\NSW-ON-NY\on-ny-nsw-kcha0642\on-ny-nsw'; 

proc sql; 
	drop table codes.procedure_codes, codes.diagnosis_codes, codes.income_postcode, codes.income_sa2, codes.postcode_sa2 ; 
quit;

/* convert to dataframe */
proc import out = codes.procedure_codes datafile = 'C:\Users\kcha0642\Documents\NSW-ON-NY\on-ny-nsw-kcha0642\on-ny-nsw\procedure_codes' 
	dbms = xlsx replace; 
	getnames = yes; 
run; 

proc import out = codes.diagnosis_codes datafile = 'C:\Users\kcha0642\Documents\NSW-ON-NY\on-ny-nsw-kcha0642\on-ny-nsw\diagnosis_codes' 
	dbms = xlsx replace; 
	getnames = yes; 
run; 

* add postcode income data here too; 
proc import out = codes.income_postcode datafile = 'C:\Users\kcha0642\Documents\NSW-ON-NY\on-ny-nsw-kcha0642\on-ny-nsw\income_postcode' 
	dbms = xlsx replace; 
	getnames = yes; 
run; 

proc import out = codes.income_sa2 datafile = 'C:\Users\kcha0642\Documents\NSW-ON-NY\on-ny-nsw-kcha0642\on-ny-nsw\income_sa2' 
	dbms = xlsx replace; 
	getnames = yes; 
run; 


/* remove punctuation */
data codes.procedure_codes; 
	set codes.procedure_codes;
	code = compress(code,"x"); 
	code = compress(code,"-"); 
	code = compress(code,"."); 
run; 
data codes.diagnosis_codes; 
	set codes.diagnosis_codes;
	code = compress(code,"x"); 
	code = compress(code,"-"); 
	code = compress(code,"."); 
run; 

/*********
* create lists 
*
* name: procedure/diagnosis list to create (should match label)
* codetable: table with codes, created above from original spreadsheet list
* edition: code edition - should match apdc variable & version. Keep at 'ICD10AM'
* 
*********/
%macro create_list (name,codetable,edition); 
proc sql noprint; 
	select quote(trim(code))
	into:&name separated by ' '
	from &codetable
	where version = &edition and label = "&name.";
quit;
%mend; 

/* create procedure code lists */ 
%global pancreatectomy prostatectomy nephrectomy; 

%create_list(pancreatectomy,codes.procedure_codes,'ICD10AM');
%put &pancreatectomy; 
%create_list(prostatectomy,codes.procedure_codes,'ICD10AM');
%put &prostatectomy; 
%create_list(nephrectomy,codes.procedure_codes,'ICD10AM');
%put &nephrectomy; 
